const app = getApp()
var core = require('../../utils/core.js')

Page({
  data: {
    avatar:"/static/images/zhanwei.png",
    userinfo:{credit2:'0.00',yestoday:'0.00'},
    scrollTop:0,
    ads:[], 
    notice:[
      {'id':1,'title':'农业农村部关于加快推进设施种植机械化发展的意见'},
      {'id':1,'title':'上海市重点工程实事立功竞赛赛区中途考核表'},
      {'id':1,'title':'2020年6月燃气器具与液化气培训班领证信息公开（20-14～18期）'},
      {'id':1,'title':'关于市住建委行政服务中心咨询电话并入市工程建设962683服务热线的通知'},
    ],
    menu:[],

    list_navigation:[
        {'id':1,'title':'猜你喜欢','choose':true},
        {'id':2,'title':'热门项目','choose':false},
        {'id':3,'title':'最热报告','choose':false},
        {'id':4,'title':'推荐企业','choose':false},
    ],
    arr: [],
    navigation_flag:false,
    navigation_index_1:-1,
    navigation_index_2:-1,
    navigation_index_3:-1,
    navigation_index_4:-1,

    navigation:[],
    triggered: false,
    sider:[],


    index:0,
    data:[],     
    page:1,
    
    project_data:[],
    project_page:1,

    news_data:[],
    news_page:1,

    company_data:[],
    company_page:1,

    localproject_data:[],
    localproject_page:1,

    list_style:'',
    noreadnum:0,
    PulibcDialog:0,
    

    DingYueFlag:false,
    qrcode:'',
    industry:[
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药1','desc':'调研全球的企业数据'},
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药2','desc':'调研全球的企业数据'},
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药3','desc':'调研全球的企业数据'},
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药4','desc':'调研全球的企业数据'},
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药5','desc':'调研全球的企业数据'},
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药6','desc':'调研全球的企业数据'},
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药7','desc':'调研全球的企业数据'},
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药8','desc':'调研全球的企业数据'},
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药9','desc':'调研全球的企业数据'},
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药10','desc':'调研全球的企业数据'},
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药11','desc':'调研全球的企业数据'},
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药12','desc':'调研全球的企业数据'},
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药13','desc':'调研全球的企业数据'},
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药14','desc':'调研全球的企业数据'},
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药15','desc':'调研全球的企业数据'},
      {'id':'1',img:'/static/images/news/qrcode.png','name':'生物医药16','desc':'调研全球的企业数据'},
    ],
    industryev:4,
    industryswiper:0,
    lat:0,
    lng:0,
  },
  onShow:function(){
    this.GetUserInfo();
  },
  setindustry:function(){
   var self=this;
   var length=self.data.industry.length
   var industryev=self.data.industryev
   var industryswiper=Math.ceil(length/industryev);
   var industry=self.data.industry
   var start=0;
   var end=0;
   var arr=[];
   var brr=[];
   for (let i = 0; i < industryswiper; i++) {
      start=i*industryev;
      end=(start+industryev-1)>(length-1)?(length-1):(start+industryev-1);
      brr=[];
      for (let j = start; j <=end; j++) {
        brr.push(industry[j]) 
      }
      arr.push({'index':i,'children':brr}) 
   }
   console.log(arr)
   self.setData({industry:arr})
  },
  onLoad:function(options){
    var self=this
    if (options != null&& options!= undefined&&options.openid) {
      wx.setStorageSync('otheropenid', options.openid) 
      wx.setStorageSync('tarbarurl', '/pages/index/index') 
    }
    

    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res)
        self.setData({lat:res.latitude,lng:res.longitude,localproject_data:[],localproject_page:1})
        self.loadingmore();
      },
      fail: function (res) {
        self.loadingmore();
      }
    })
    var self=this;
    app.getnoreadtotal().then(res => {
      self.setData({noreadnum:res})
    })
  },
  loadingmore(){
    var self=this;
    self.GetUserInfo();
    self.getAds()
    self.getNavs()
    self.getList();
    self.getProjectList();
    self.getLocalprojectList();
    self.getNewsList();
    self.getCompanyList();
    self.GetChoose();
    self.Getindustry();
  },
  Getindustry:function(){
    var self=this;
    core.post("label",{op:'OnlineIndustry'},function(res){
      if(res.code==1){
         self.setData({industry:res.data.data})
         self.setindustry()
      }
    })
  },
  onPullDownRefresh: function () {
    this.setData({
      page:1,
      data:[],
      project_data:[],
      project_page:1,
      news_data:[],
      news_page:1,
      company_data:[],
      company_page:1,
      localproject_data:[],
      localproject_page:1
    })
    this.getList();
    this.getAds()
    this.getNavs()
    this.getProjectList();
    this.getNewsList();
    this.getCompanyList();
    this.getLocalprojectList();
    this.GetChoose()
  },

  onPulling(e) {
  },
  onRestore(e) {
  },

  onAbort(e) {
  },
  getAds:function(){
    var self=this
    core.post("Ads",{op:'ads'},function(res){
      if(res.code==1){
         self.setData({ads:res.data})
      }
    })
  },
  getNavs:function(){
    var self=this
    core.post("Ads",{op:'navs'},function(res){
      if(res.code==1){
         self.setData({menu:res.data})
      }
    })
  },
    getList:function(){
      
      //this.data=this.data1
      wx.showLoading({
        title: '加载中',
      })
      var self=this
      core.post("ProjectList",{page:self.data.page},function(res){
        var data=self.data.data
        if(res.code==1){
           for (let i = 0; i < res.data.length; i++) {
               data.push(res.data[i])             
           }
           self.setData({data:data,page:self.data.page+1})
        }
        wx.hideLoading();
      })
      
  },
  getProjectList:function(){
    var self=this
    core.post("ProjectList",{page:self.data.project_page},function(res){
      var data=self.data.project_data
      if(res.code==1){
         for (let i = 0; i < res.data.length; i++) {
             data.push(res.data[i])             
         }
         self.setData({project_data:data,project_page:self.data.project_page+1})
      }
    })
    
},
getNewsList:function(){
  var self=this
  core.post("News",{page:self.data.news_page,op:'index'},function(res){
    var data=self.data.news_data
    if(res.code==1){
       for (let i = 0; i < res.data.length; i++) {
           data.push(res.data[i])             
       }
       self.setData({news_data:data,page:self.data.news_page+1})
    }
  })
},
    /**
   * list
   */
  getCompanyList:function(){
   
    var self=this
    var data={
      page:self.data.company_page,
      op:'list'
    }
    core.post("Company",data,function(res){
      var data=self.data.company_data
      if(res.code==1){
        if(res.data.length>0){
          for (let i = 0; i < res.data.length; i++) {            
              data.push(res.data[i])             
          }
          self.setData({company_data:data,page:self.data.company_page+1})
        }
      }
      wx.stopPullDownRefresh()
    })
  },
getLocalprojectList:function(){
      
    //this.data=this.data1
    wx.showLoading({
      title: '加载中',
    })
    var self=this
    var data={
      page:self.data.localproject_page,
      lat:self.data.lat,
      lng:self.data.lng,
      op:'list'
    }
    core.post("LocalProjects",data,function(res){
      var data=self.data.localproject_data
      if(res.code==1){
         for (let i = 0; i < res.data.length; i++) {
             data.push(res.data[i])             
         }
         self.setData({localproject_data:data,localproject_page:self.data.localproject_page+1})
      }  
      wx.stopPullDownRefresh() 
    })
    
},
  release:function(){
    wx.navigateTo({
      url: '/pages/release/release'　
    })
  },
  /**
   * 选择筛选类型
   */
  choose_type:function(res){
    var data=res.currentTarget.id.split('&&')
    var index=data[1]
    
    this.setData({
      [index]:data[0]
    })
    var level=data[1].split('index_')[1]*1
    
    for (let i =level+1; i <=4; i++) {
      index= 'navigation_index_'+i
      this.setData({
        [index]:-1
      }) 
    }
    //把本级true  下级变成false
    var self=this
    

    var data_lin=[];
    var data_str=''
    var data_arr=[];
    if(level*1==1){
      data_lin=self.data.navigation;
      for (let i = 0; i < data_lin.length; i++) {
        if(i==data[0]) {
            if(data_lin[i].flag==false){
              data_lin[i].flag=true
              this.setData({navigation_flag:1})
            }else{
              data_lin[i].flag=false
              this.setData({navigation_flag:0})
            }
        } else{
           data_lin[i].flag=false;
        }     
      }
      // data_str=JSON.stringify(data_lin[self.data.navigation_index_1].children)
      // data_arr=JSON.parse(data_str.replace(/"flag":true/g, '"flag":false'))
      // data_lin[self.data.navigation_index_1].children=data_arr
      // data_lin[self.data.navigation_index_1].children[data[0]].flag=true
      //self.setData({navigation:data_lin})

      self.setData({navigation:data_lin})
     
    }
    if(level*1==2){
      data_lin=self.data.navigation;
      data_str=JSON.stringify(data_lin[self.data.navigation_index_1].children)
      data_arr=JSON.parse(data_str.replace(/"flag":true/g, '"flag":false'))
      data_lin[self.data.navigation_index_1].children=data_arr
      data_lin[self.data.navigation_index_1].children[data[0]].flag=true
      self.setData({navigation:data_lin})
    }
    if(level*1==3){
      data_lin=self.data.navigation;
      data_str=JSON.stringify(data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children)
      data_arr=JSON.parse(data_str.replace(/"flag":true/g, '"flag":false'))
      data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children=data_arr
      data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children[data[0]].flag=true
      self.setData({navigation:data_lin})
    }
    if(level*1==4){
      data_lin=self.data.navigation;
      data_str=JSON.stringify(data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children)
      data_arr=JSON.parse(data_str.replace(/"flag":true/g, '"flag":false'))
      data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children=data_arr
      data_lin[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children[data[0]].flag=true
      self.setData({navigation:data_lin})
    }
   
  },
  GetChoose(){
    var self=this
    core.post("Choose",{},function(res){
      if(res.code==1){
          self.setData({
            navigation:res.data,
          })
        
      }
    })
  },
  choose_con:function(target){
    var id=target.currentTarget.id;
    var self=this
    var data=self.data.navigation;
    if(id==0){
       data[self.data.navigation_index_1].flag=false;
       this.setData({navigation:data,navigation_flag:false})
    }
    if(id==1){
      
      var name=[];
      

      if(self.data.navigation_index_2!=-1&&data[self.data.navigation_index_1].children[self.data.navigation_index_2]){
        name[name.length]=data[self.data.navigation_index_1].children[self.data.navigation_index_2].name
      }
      if(self.data.navigation_index_3!=-1&&data[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3]){
        name[name.length]=data[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].name
      }
      if(self.data.navigation_index_4!=-1&&data[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children[self.data.navigation_index_4]){
        name[name.length]=data[self.data.navigation_index_1].children[self.data.navigation_index_2].children[self.data.navigation_index_3].children[self.data.navigation_index_4].name
      }
      data[self.data.navigation_index_1].flag=false;
      data[self.data.navigation_index_1].name=name.join(",");
      

      var data_str=JSON.stringify(data)
      var data_arr=JSON.parse(data_str.replace(/"flag":true/g, '"flag":false'))
      
      self.setData({navigation:data_arr,navigation_flag:false,navigation_index_1:-1,navigation_index_2:-1,navigation_index_3:-1,navigation_index_4:-1})

   }
  },
    //选项卡点击
change:function(e){
      this.setData({index:e.target.id})
  },
  //触摸滑动
  touch_change:function(e){
    // return
    this.setData({index:e.detail.current})
  },
    //监听屏幕滚动 判断上下滚动
    onPageScroll: function (ev) {
      var _this = this;
      _this.setData({scrollTop:ev.scrollTop});
      //console.log(ev.scrollTop)
      if(ev.scrollTop>=590){
        _this.setData({list_style:'overflow: auto;height: 1300rpx;'});
      }else{
        _this.setData({list_style:''});   
      }
    },
    /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return this.share()
  },
    /**
   * 用户点击右上角分享
   */
  onShareTimeline: function () {
    return this.share()
  },
  share:function(){
    return {
      title:  '投资项目 发上来赚钱；招商项目 发上来推广',
      path: '/pages/index/index?openid=' +wx.getStorageSync('openid1'),
      imageUrl:  app.globalData.shareIndeximg//自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径。支持PNG及JPG。显示图片长宽比是 5:4。
    } 
  },
  /**
   * 获得用户信息
   */
  GetUserInfo:function(){
    var self=this
    core.post("GetUserinfo",{},function(res){
      if(res.code==1){
           self.setData({           
             avatar:res.data.avatar,  
             userinfo: res.data   
           })
      }
    })
  },
  person:function(){
    wx.switchTab({
      url: "/pages/person/person",
   });
  },
  PulibcDialogHide:function(){
    this.setData({PulibcDialog:0})
  },
  PulibcDialogShow:function(){
    console.log(333)
    this.setData({PulibcDialog:1})
  },
  ShowDingYue:function(){ 
    wx.showLoading({
      title: '努力生成总',
    })
    var self=this
    core.post("qrcode",{op:'WxQrcode'},function(res){
      if(res.code==1){
        wx.hideLoading();
        self.setData({DingYueFlag:true,qrcode:res.data.qrcode})
      }
    })
  },
  HideDingYue:function(){
    this.setData({DingYueFlag:false})
  },
  /**
   * 二维码保存
   */
  save_qrcode:function(){
    var self=this
    wx.getImageInfo({
      src: self.data.qrcode,
      success: function (ret) {
          var path = ret.path;
          wx.saveImageToPhotosAlbum({
              filePath: path,
              success(result) {
                wx.showToast({
                  'title':'保存成功',
                  'icon':'success'
                })
              }
          })
      }
    })
  },
  /**
   * 预览二维码
   */
  WxQrcode:function(){
    var imgArr = [this.data.qrcode];
    wx.previewImage({
      current:this.data.qrcode,     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  MainNavigator:function(res){
    if(res.currentTarget.dataset.out=='0'){
       wx.navigateTo({
         url: res.currentTarget.dataset.url
       })
    }else{
      wx.navigateTo({
        url: '/pages/index/outurl?url='+encodeURIComponent(res.currentTarget.dataset.url)
      })
    }
    console.log(res)
  }
})
